using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace MatrixFramework.Shared.Domain.Photos.Metadata
{
    /// <summary>
    /// 照片元数据提取实体
    /// 支持从各种来源提取和管理照片元数据
    /// </summary>
    public class PhotoMetadataExtraction : FullAuditedEntity<Guid>, IMultiTenant
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public virtual Guid? TenantId { get; protected set; }

        /// <summary>
        /// 照片ID
        /// </summary>
        public virtual Guid PhotoId { get; protected set; }

        /// <summary>
        /// 提取类型
        /// </summary>
        public virtual ExtractionType ExtractionType { get; protected set; }

        /// <summary>
        /// 提取状态
        /// </summary>
        public virtual ExtractionStatus Status { get; protected set; }

        /// <summary>
        /// 提取引擎
        /// </summary>
        [Required]
        [MaxLength(100)]
        public virtual string Engine { get; protected set; }

        /// <summary>
        /// 引擎版本
        /// </summary>
        [MaxLength(50)]
        public virtual string EngineVersion { get; protected set; }

        /// <summary>
        /// 提取时间
        /// </summary>
        public virtual DateTime ExtractionTime { get; protected set; }

        /// <summary>
        /// 执行耗时（毫秒）
        /// </summary>
        public virtual long ExecutionDuration { get; protected set; }

        /// <summary>
        /// 提取参数
        /// </summary>
        public virtual Dictionary<string, object> Parameters { get; protected set; }

        /// <summary>
        /// 提取结果
        /// </summary>
        public virtual ExtractionResult Result { get; protected set; }

        /// <summary>
        /// 元数据来源
        /// </summary>
        public virtual List<MetadataSource> Sources { get; protected set; }

        /// <summary>
        /// 提取统计
        /// </summary>
        public virtual ExtractionStatistics Statistics { get; protected set; }

        /// <summary>
        /// 提取配置
        /// </summary>
        public virtual ExtractionConfiguration Configuration { get; protected set; }

        /// <summary>
        /// 提取日志
        /// </summary>
        public virtual List<ExtractionLog> Logs { get; protected set; }

        /// <summary>
        /// 照片导航属性
        /// </summary>
        public virtual Photo Photo { get; protected set; }

        protected PhotoMetadataExtraction()
        {
            Parameters = new Dictionary<string, object>();
            Result = new ExtractionResult();
            Sources = new List<MetadataSource>();
            Statistics = new ExtractionStatistics();
            Configuration = new ExtractionConfiguration();
            Logs = new List<ExtractionLog>();
        }

        public PhotoMetadataExtraction(
            Guid id,
            Guid photoId,
            ExtractionType extractionType,
            string engine = "Default",
            Dictionary<string, object> parameters = null,
            Guid? tenantId = null) : base(id)
        {
            PhotoId = photoId;
            ExtractionType = extractionType;
            Engine = engine;
            Parameters = parameters ?? new Dictionary<string, object>();
            TenantId = tenantId;
            Status = ExtractionStatus.Pending;
            ExtractionTime = DateTime.Now;

            Result = new ExtractionResult();
            Sources = new List<MetadataSource>();
            Statistics = new ExtractionStatistics();
            Configuration = new ExtractionConfiguration();
            Logs = new List<ExtractionLog>();
        }

        /// <summary>
        /// 开始提取
        /// </summary>
        public virtual void StartExtraction()
        {
            if (Status != ExtractionStatus.Pending && Status != ExtractionStatus.Retrying)
                throw new InvalidOperationException($"无法在状态 {Status} 下开始提取");

            Status = ExtractionStatus.Running;
            ExtractionTime = DateTime.Now;
            AddLog(ExtractionLogLevel.Info, "开始提取元数据", $"提取类型: {ExtractionType}");
        }

        /// <summary>
        /// 完成提取
        /// </summary>
        public virtual void CompleteExtraction(ExtractionResult result, long executionDuration)
        {
            Status = ExtractionStatus.Completed;
            Result = result ?? new ExtractionResult();
            ExecutionDuration = executionDuration;

            // 更新统计信息
            Statistics.UpdateStatistics(this);

            AddLog(ExtractionLogLevel.Info, "元数据提取完成", $"耗时: {executionDuration}ms, 提取字段数: {Result.ExtractedFields?.Count ?? 0}");
        }

        /// <summary>
        /// 提取失败
        /// </summary>
        public virtual void FailExtraction(string errorMessage)
        {
            Status = ExtractionStatus.Failed;
            Result.ErrorMessage = errorMessage;
            AddLog(ExtractionLogLevel.Error, "元数据提取失败", errorMessage);
        }

        /// <summary>
        /// 取消提取
        /// </summary>
        public virtual void CancelExtraction(string reason = null)
        {
            Status = ExtractionStatus.Cancelled;
            AddLog(ExtractionLogLevel.Warning, "元数据提取已取消", reason);
        }

        /// <summary>
        /// 重试提取
        /// </summary>
        public virtual void RetryExtraction()
        {
            if (Statistics.RetryCount >= Configuration.MaxRetryCount)
            {
                FailExtraction("已达到最大重试次数");
                return;
            }

            Statistics.RetryCount++;
            Status = ExtractionStatus.Retrying;
            ExtractionTime = DateTime.Now;
            Result.ErrorMessage = null;
            AddLog(ExtractionLogLevel.Info, "开始重试提取", $"重试次数: {Statistics.RetryCount}/{Configuration.MaxRetryCount}");
        }

        /// <summary>
        /// 添加元数据来源
        /// </summary>
        public virtual void AddSource(MetadataSource source)
        {
            if (source != null)
            {
                Sources.Add(source);
            }
        }

        /// <summary>
        /// 更新提取结果
        /// </summary>
        public virtual void UpdateResult(ExtractionResult result)
        {
            Result = result ?? new ExtractionResult();
        }

        /// <summary>
        /// 更新参数
        /// </summary>
        public virtual void UpdateParameters(Dictionary<string, object> parameters)
        {
            Parameters = parameters ?? new Dictionary<string, object>();
        }

        /// <summary>
        /// 设置参数
        /// </summary>
        public virtual void SetParameter(string key, object value)
        {
            Parameters[key] = value;
        }

        /// <summary>
        /// 更新配置
        /// </summary>
        public virtual void UpdateConfiguration(ExtractionConfiguration configuration)
        {
            Configuration = configuration ?? new ExtractionConfiguration();
        }

        /// <summary>
        /// 添加提取日志
        /// </summary>
        public virtual void AddLog(ExtractionLogLevel level, string message, string details = null)
        {
            var log = new ExtractionLog
            {
                Level = level,
                Message = message,
                Details = details,
                LogTime = DateTime.Now
            };

            Logs.Add(log);
        }

        /// <summary>
        /// 检查是否可以重试
        /// </summary>
        public virtual bool CanRetry()
        {
            return Statistics.RetryCount < Configuration.MaxRetryCount &&
                   (Status == ExtractionStatus.Failed || Status == ExtractionStatus.Timeout);
        }

        /// <summary>
        /// 检查是否已完成
        /// </summary>
        public virtual bool IsCompleted()
        {
            return Status == ExtractionStatus.Completed ||
                   Status == ExtractionStatus.Failed ||
                   Status == ExtractionStatus.Cancelled;
        }

        /// <summary>
        /// 获取提取摘要
        /// </summary>
        public virtual ExtractionSummary GetSummary()
        {
            return new ExtractionSummary
            {
                PhotoId = PhotoId,
                ExtractionType = ExtractionType,
                Status = Status,
                Engine = Engine,
                ExecutionDuration = ExecutionDuration,
                ExtractedFieldsCount = Result.ExtractedFields?.Count ?? 0,
                SourcesCount = Sources.Count,
                SuccessRate = Statistics.SuccessRate,
                LastExtractionTime = ExtractionTime
            };
        }
    }

    /// <summary>
    /// 提取类型枚举
    /// </summary>
    public enum ExtractionType
    {
        /// <summary>
        /// EXIF数据提取
        /// </summary>
        EXIF = 0,

        /// <summary>
        /// IPTC数据提取
        /// </summary>
        IPTC = 1,

        /// <summary>
        /// XMP数据提取
        /// </summary>
        XMP = 2,

        /// <summary>
        /// 文件系统元数据提取
        /// </summary>
        FileSystem = 3,

        /// <summary>
        /// 图像技术元数据提取
        /// </summary>
        ImageTechnical = 4,

        /// <summary>
        /// 颜色元数据提取
        /// </summary>
        ColorMetadata = 5,

        /// <summary>
        /// 地理位置元数据提取
        /// </summary>
        Geographic = 6,

        /// <summary>
        /// 相机元数据提取
        /// </summary>
        Camera = 7,

        /// <summary>
        /// 软件元数据提取
        /// </summary>
        Software = 8,

        /// <summary>
        /// 版权元数据提取
        /// </summary>
        Copyright = 9,

        /// <summary>
        /// 自定义元数据提取
        /// </summary>
        Custom = 10,

        /// <summary>
        /// 视频元数据提取
        /// </summary>
        Video = 11,

        /// <summary>
        /// 音频元数据提取
        /// </summary>
        Audio = 12,

        /// <summary>
        /// RAW文件元数据提取
        /// </summary>
        RAW = 13,

        /// <summary>
        /// PDF元数据提取
        /// </summary>
        PDF = 14,

        /// <summary>
        /// 文档元数据提取
        /// </summary>
        Document = 15,

        /// <summary>
        /// 综合提取
        /// </summary>
        Comprehensive = 16
    }

    /// <summary>
    /// 提取状态枚举
    /// </summary>
    public enum ExtractionStatus
    {
        /// <summary>
        /// 等待中
        /// </summary>
        Pending = 0,

        /// <summary>
        /// 运行中
        /// </summary>
        Running = 1,

        /// <summary>
        /// 已完成
        /// </summary>
        Completed = 2,

        /// <summary>
        /// 失败
        /// </summary>
        Failed = 3,

        /// <summary>
        /// 已取消
        /// </summary>
        Cancelled = 4,

        /// <summary>
        /// 重试中
        /// </summary>
        Retrying = 5,

        /// <summary>
        /// 超时
        /// </summary>
        Timeout = 6,

        /// <summary>
        /// 部分成功
        /// </summary>
        PartialSuccess = 7
    }

    /// <summary>
    /// 提取结果
    /// </summary>
    public class ExtractionResult
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage { get; set; }

        /// <summary>
        /// 提取的字段
        /// </summary>
        public Dictionary<string, object> ExtractedFields { get; set; } = new();

        /// <summary>
        /// 提取的EXIF数据
        /// </summary>
        public EXIFData EXIFData { get; set; }

        /// <summary>
        /// 提取的IPTC数据
        /// </summary>
        public IPTCData IPTCData { get; set; }

        /// <summary>
        /// 提取的XMP数据
        /// </summary>
        public XMPData XMPData { get; set; }

        /// <summary>
        /// 提取的文件系统数据
        /// </summary>
        public FileSystemData FileSystemData { get; set; }

        /// <summary>
        /// 提取的图像技术数据
        /// </summary>
        public ImageTechnicalData ImageTechnicalData { get; set; }

        /// <summary>
        /// 提取的地理数据
        /// </summary>
        public GeographicData GeographicData { get; set; }

        /// <summary>
        /// 提取的相机数据
        /// </summary>
        public CameraData CameraData { get; set; }

        /// <summary>
        /// 提取的软件数据
        /// </summary>
        public SoftwareData SoftwareData { get; set; }

        /// <summary>
        /// 提取的版权数据
        /// </summary>
        public CopyrightData CopyrightData { get; set; }

        /// <summary>
        /// 提取的视频数据
        /// </summary>
        public VideoData VideoData { get; set; }

        /// <summary>
        /// 提取的音频数据
        /// </summary>
        public AudioData AudioData { get; set; }

        /// <summary>
        /// 原始数据
        /// </summary>
        public byte[] RawData { get; set; }

        /// <summary>
        /// 数据完整性验证结果
        /// </summary>
        public DataIntegrityResult IntegrityResult { get; set; }

        /// <summary>
        /// 提取质量评分
        /// </summary>
        public double QualityScore { get; set; }

        /// <summary>
        /// 缺失字段列表
        /// </summary>
        public List<string> MissingFields { get; set; } = new();

        /// <summary>
        /// 警告信息
        /// </summary>
        public List<string> Warnings { get; set; } = new();

        /// <summary>
        /// 获取所有元数据的合并视图
        /// </summary>
        public Dictionary<string, object> GetAllMetadata()
        {
            var allMetadata = new Dictionary<string, object>(ExtractedFields);

            // 添加各种类型的数据
            AddMetadataToDictionary(allMetadata, "EXIF", EXIFData?.ToDictionary());
            AddMetadataToDictionary(allMetadata, "IPTC", IPTCData?.ToDictionary());
            AddMetadataToDictionary(allMetadata, "XMP", XMPData?.ToDictionary());
            AddMetadataToDictionary(allMetadata, "FileSystem", FileSystemData?.ToDictionary());
            AddMetadataToDictionary(allMetadata, "ImageTechnical", ImageTechnicalData?.ToDictionary());
            AddMetadataToDictionary(allMetadata, "Geographic", GeographicData?.ToDictionary());
            AddMetadataToDictionary(allMetadata, "Camera", CameraData?.ToDictionary());
            AddMetadataToDictionary(allMetadata, "Software", SoftwareData?.ToDictionary());
            AddMetadataToDictionary(allMetadata, "Copyright", CopyrightData?.ToDictionary());

            return allMetadata;
        }

        /// <summary>
        /// 添加元数据到字典
        /// </summary>
        private void AddMetadataToDictionary(Dictionary<string, object> target, string prefix, Dictionary<string, object> source)
        {
            if (source == null) return;

            foreach (var kvp in source)
            {
                target[$"{prefix}_{kvp.Key}"] = kvp.Value;
            }
        }
    }

    /// <summary>
    /// 元数据来源
    /// </summary>
    public class MetadataSource
    {
        /// <summary>
        /// 来源ID
        /// </summary>
        public Guid Id { get; set; } = Guid.NewGuid();

        /// <summary>
        /// 来源类型
        /// </summary>
        public MetadataSourceType SourceType { get; set; }

        /// <summary>
        /// 来源名称
        /// </summary>
        public string SourceName { get; set; }

        /// <summary>
        /// 来源版本
        /// </summary>
        public string SourceVersion { get; set; }

        /// <summary>
        /// 可信度
        /// </summary>
        public double Reliability { get; set; }

        /// <summary>
        /// 提取时间
        /// </summary>
        public DateTime ExtractionTime { get; set; }

        /// <summary>
        /// 提取的字段数量
        /// </summary>
        public int ExtractedFieldsCount { get; set; }

        /// <summary>
        /// 是否为主要来源
        /// </summary>
        public bool IsPrimary { get; set; }

        /// <summary>
        /// 来源状态
        /// </summary>
        public SourceStatus Status { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string ErrorMessage { get; set; }

        /// <summary>
        /// 提取的原始数据大小（字节）
        /// </summary>
        public long RawDataSize { get; set; }

        /// <summary>
        /// 处理耗时（毫秒）
        /// </summary>
        public long ProcessingTimeMs { get; set; }
    }

    /// <summary>
    /// 元数据来源类型枚举
    /// </summary>
    public enum MetadataSourceType
    {
        /// <summary>
        /// 文件头
        /// </summary>
        FileHeader = 0,

        /// <summary>
        /// EXIF标签
        /// </summary>
        EXIFTags = 1,

        /// <summary>
        /// IPTC标签
        /// </summary>
        IPTCTags = 2,

        /// <summary>
        /// XMP数据包
        /// </summary>
        XMPacket = 3,

        /// <summary>
        /// 文件系统
        /// </summary>
        FileSystem = 4,

        /// <summary>
        /// 图像分析
        /// </summary>
        ImageAnalysis = 5,

        /// <summary>
        /// 外部数据库
        /// </summary>
        ExternalDatabase = 6,

        /// <summary>
        /// 用户输入
        /// </summary>
        UserInput = 7,

        /// <summary>
        /// AI分析
        /// </summary>
        AIAnalysis = 8,

        /// <summary>
        /// 第三方服务
        /// </summary>
        ThirdPartyService = 9,

        /// <summary>
        /// 云存储服务
        /// </summary>
        CloudStorage = 10
    }

    /// <summary>
    /// 来源状态枚举
    /// </summary>
    public enum SourceStatus
    {
        /// <summary>
        /// 可用
        /// </summary>
        Available = 0,

        /// <summary>
        /// 不可用
        /// </summary>
        Unavailable = 1,

        /// <summary>
        /// 部分可用
        /// </summary>
        PartiallyAvailable = 2,

        /// <summary>
        /// 已损坏
        /// </summary>
        Corrupted = 3,

        /// <summary>
        /// 不支持
        /// </summary>
        NotSupported = 4
    }

    /// <summary>
    /// 提取统计
    /// </summary>
    public class ExtractionStatistics
    {
        /// <summary>
        /// 总提取次数
        /// </summary>
        public int TotalExtractions { get; set; }

        /// <summary>
        /// 成功次数
        /// </summary>
        public int SuccessCount { get; set; }

        /// <summary>
        /// 失败次数
        /// </summary>
        public int FailureCount { get; set; }

        /// <summary>
        /// 重试次数
        /// </summary>
        public int RetryCount { get; set; }

        /// <summary>
        /// 平均执行时间（毫秒）
        /// </summary>
        public double AverageExecutionTimeMs { get; set; }

        /// <summary>
        /// 最小执行时间（毫秒）
        /// </summary>
        public long MinExecutionTimeMs { get; set; }

        /// <summary>
        /// 最大执行时间（毫秒）
        /// </summary>
        public long MaxExecutionTimeMs { get; set; }

        /// <summary>
        /// 总提取字段数
        /// </summary>
        public int TotalExtractedFields { get; set; }

        /// <summary>
        /// 平均提取字段数
        /// </summary>
        public double AverageExtractedFields { get; set; }

        /// <summary>
        /// 最后提取时间
        /// </summary>
        public DateTime? LastExtractionTime { get; set; }

        /// <summary>
        /// 成功率
        /// </summary>
        public double SuccessRate => TotalExtractions > 0 ? (double)SuccessCount / TotalExtractions : 0;

        /// <summary>
        /// 更新统计信息
        /// </summary>
        public void UpdateStatistics(PhotoMetadataExtraction extraction)
        {
            TotalExtractions++;

            if (extraction.Status == ExtractionStatus.Completed)
            {
                SuccessCount++;
            }
            else
            {
                FailureCount++;
            }

            if (extraction.ExecutionDuration > 0)
            {
                AverageExecutionTimeMs = (AverageExecutionTimeMs * (TotalExtractions - 1) + extraction.ExecutionDuration) / TotalExtractions;

                if (MinExecutionTimeMs == 0 || extraction.ExecutionDuration < MinExecutionTimeMs)
                    MinExecutionTimeMs = extraction.ExecutionDuration;

                if (extraction.ExecutionDuration > MaxExecutionTimeMs)
                    MaxExecutionTimeMs = extraction.ExecutionDuration;
            }

            var extractedFieldsCount = extraction.Result?.ExtractedFields?.Count ?? 0;
            TotalExtractedFields += extractedFieldsCount;
            AverageExtractedFields = (double)TotalExtractedFields / TotalExtractions;

            LastExtractionTime = extraction.ExtractionTime;
        }
    }

    /// <summary>
    /// 提取配置
    /// </summary>
    public class ExtractionConfiguration
    {
        /// <summary>
        /// 最大重试次数
        /// </summary>
        public int MaxRetryCount { get; set; } = 3;

        /// <summary>
        /// 超时时间（秒）
        /// </summary>
        public int TimeoutSeconds { get; set; } = 30;

        /// <summary>
        /// 是否启用缓存
        /// </summary>
        public bool EnableCache { get; set; } = true;

        /// <summary>
        /// 缓存有效期（小时）
        /// </summary>
        public int CacheExpiryHours { get; set; } = 24;

        /// <summary>
        /// 是否启用深度提取
        /// </summary>
        public bool EnableDeepExtraction { get; set; } = false;

        /// <summary>
        /// 是否验证数据完整性
        /// </summary>
        public bool ValidateIntegrity { get; set; } = true;

        /// <summary>
        /// 是否提取原始数据
        /// </summary>
        public bool ExtractRawData { get; set; } = false;

        /// <summary>
        /// 最大原始数据大小（MB）
        /// </summary>
        public int MaxRawDataSizeMB { get; set; } = 10;

        /// <summary>
        /// 提取优先级列表
        /// </summary>
        public List<ExtractionType> PriorityOrder { get; set; } = new();

        /// <summary>
        /// 排除的提取类型
        /// </summary>
        public List<ExtractionType> ExcludedTypes { get; set; } = new();

        /// <summary>
        /// 字段映射配置
        /// </summary>
        public Dictionary<string, string> FieldMapping { get; set; } = new();

        /// <summary>
        /// 数据清理规则
        /// </summary>
        public List<DataCleaningRule> CleaningRules { get; set; } = new();

        /// <summary>
        /// 质量阈值
        /// </summary>
        public QualityThresholds QualityThresholds { get; set; } = new();
    }

    /// <summary>
    /// 数据清理规则
    /// </summary>
    public class DataCleaningRule
    {
        /// <summary>
        /// 规则名称
        /// </summary>
        public string RuleName { get; set; }

        /// <summary>
        /// 目标字段
        /// </summary>
        public string TargetField { get; set; }

        /// <summary>
        /// 规则类型
        /// </summary>
        public CleaningRuleType RuleType { get; set; }

        /// <summary>
        /// 规则参数
        /// </summary>
        public Dictionary<string, object> Parameters { get; set; } = new();

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnabled { get; set; } = true;

        /// <summary>
        /// 优先级
        /// </summary>
        public int Priority { get; set; }
    }

    /// <summary>
    /// 清理规则类型枚举
    /// </summary>
    public enum CleaningRuleType
    {
        /// <summary>
        /// 格式化日期
        /// </summary>
        FormatDate = 0,

        /// <summary>
        /// 清理空白字符
        /// </summary>
        TrimWhitespace = 1,

        /// <summary>
        /// 转换编码
        /// </summary>
        ConvertEncoding = 2,

        /// <summary>
        /// 标准化坐标
        /// </summary>
        NormalizeCoordinates = 3,

        /// <summary>
        /// 验证范围
        /// </summary>
        ValidateRange = 4,

        /// <summary>
        /// 默认值填充
        /// </summary>
        FillDefaultValue = 5,

        /// <summary>
        /// 正则表达式清理
        /// </summary>
        RegexCleanup = 6,

        /// <summary>
        /// 单位转换
        /// </summary>
        ConvertUnits = 7,

        /// <summary>
        /// 自定义清理
        /// </summary>
        Custom = 8
    }

    /// <summary>
    /// 质量阈值
    /// </summary>
    public class QualityThresholds
    {
        /// <summary>
        /// 最小质量评分
        /// </summary>
        public double MinQualityScore { get; set; } = 0.5;

        /// <summary>
        /// 最大警告数量
        /// </summary>
        public int MaxWarningCount { get; set; } = 10;

        /// <summary>
        /// 最大缺失字段数量
        /// </summary>
        public int MaxMissingFields { get; set; } = 20;

        /// <summary>
        /// 必需字段列表
        /// </summary>
        public List<string> RequiredFields { get; set; } = new();

        /// <summary>
        /// 可选字段列表
        /// </summary>
        public List<string> OptionalFields { get; set; } = new();
    }

    /// <summary>
    /// 提取日志
    /// </summary>
    public class ExtractionLog
    {
        /// <summary>
        /// 日志级别
        /// </summary>
        public ExtractionLogLevel Level { get; set; }

        /// <summary>
        /// 日志消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 详细信息
        /// </summary>
        public string Details { get; set; }

        /// <summary>
        /// 日志时间
        /// </summary>
        public DateTime LogTime { get; set; }

        /// <summary>
        /// 相关字段
        /// </summary>
        public string RelatedField { get; set; }

        /// <summary>
        /// 相关来源
        /// </summary>
        public string RelatedSource { get; set; }

        /// <summary>
        /// 额外数据
        /// </summary>
        public Dictionary<string, object> AdditionalData { get; set; } = new();
    }

    /// <summary>
    /// 提取日志级别枚举
    /// </summary>
    public enum ExtractionLogLevel
    {
        /// <summary>
        /// 调试
        /// </summary>
        Debug = 0,

        /// <summary>
        /// 信息
        /// </summary>
        Info = 1,

        /// <summary>
        /// 警告
        /// </summary>
        Warning = 2,

        /// <summary>
        /// 错误
        /// </summary>
        Error = 3,

        /// <summary>
        /// 致命错误
        /// </summary>
        Fatal = 4
    }

    /// <summary>
    /// 提取摘要
    /// </summary>
    public class ExtractionSummary
    {
        /// <summary>
        /// 照片ID
        /// </summary>
        public Guid PhotoId { get; set; }

        /// <summary>
        /// 提取类型
        /// </summary>
        public ExtractionType ExtractionType { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public ExtractionStatus Status { get; set; }

        /// <summary>
        /// 引擎
        /// </summary>
        public string Engine { get; set; }

        /// <summary>
        /// 执行耗时
        /// </summary>
        public long ExecutionDuration { get; set; }

        /// <summary>
        /// 提取字段数量
        /// </summary>
        public int ExtractedFieldsCount { get; set; }

        /// <summary>
        /// 来源数量
        /// </summary>
        public int SourcesCount { get; set; }

        /// <summary>
        /// 成功率
        /// </summary>
        public double SuccessRate { get; set; }

        /// <summary>
        /// 最后提取时间
        /// </summary>
        public DateTime LastExtractionTime { get; set; }

        /// <summary>
        /// 状态描述
        /// </summary>
        public string StatusDescription => GetStatusDescription();

        /// <summary>
        /// 获取状态描述
        /// </summary>
        private string GetStatusDescription()
        {
            return Status switch
            {
                ExtractionStatus.Pending => "等待中",
                ExtractionStatus.Running => "运行中",
                ExtractionStatus.Completed => "已完成",
                ExtractionStatus.Failed => "失败",
                ExtractionStatus.Cancelled => "已取消",
                ExtractionStatus.Retrying => "重试中",
                ExtractionStatus.Timeout => "超时",
                ExtractionStatus.PartialSuccess => "部分成功",
                _ => "未知状态"
            };
        }
    }

    // 各种元数据类型的定义（简化版，实际应用中可以扩展）

    /// <summary>
    /// EXIF数据
    /// </summary>
    public class EXIFData
    {
        public Dictionary<string, object> ToDictionary()
        {
            return new Dictionary<string, object>
            {
                ["Make"] = Make,
                ["Model"] = Model,
                ["DateTimeOriginal"] = DateTimeOriginal,
                ["ExposureTime"] = ExposureTime,
                ["FNumber"] = FNumber,
                ["ISO"] = ISO,
                ["FocalLength"] = FocalLength,
                ["Flash"] = Flash,
                ["WhiteBalance"] = WhiteBalance
            };
        }

        public string Make { get; set; }
        public string Model { get; set; }
        public DateTime? DateTimeOriginal { get; set; }
        public string ExposureTime { get; set; }
        public string FNumber { get; set; }
        public int ISO { get; set; }
        public string FocalLength { get; set; }
        public string Flash { get; set; }
        public string WhiteBalance { get; set; }
    }

    /// <summary>
    /// IPTC数据
    /// </summary>
    public class IPTCData
    {
        public Dictionary<string, object> ToDictionary()
        {
            return new Dictionary<string, object>
            {
                ["Caption"] = Caption,
                ["Keywords"] = Keywords,
                ["Copyright"] = Copyright,
                ["ByLine"] = ByLine,
                ["Credit"] = Credit,
                ["Source"] = Source,
                ["ObjectName"] = ObjectName,
                ["DateCreated"] = DateCreated
            };
        }

        public string Caption { get; set; }
        public List<string> Keywords { get; set; } = new();
        public string Copyright { get; set; }
        public string ByLine { get; set; }
        public string Credit { get; set; }
        public string Source { get; set; }
        public string ObjectName { get; set; }
        public DateTime? DateCreated { get; set; }
    }

    /// <summary>
    /// XMP数据
    /// </summary>
    public class XMPData
    {
        public Dictionary<string, object> ToDictionary()
        {
            return new Dictionary<string, object>
            {
                ["Title"] = Title,
                ["Description"] = Description,
                ["Subject"] = Subject,
                ["Creator"] = Creator,
                ["Rights"] = Rights,
                ["CreateDate"] = CreateDate,
                ["ModifyDate"] = ModifyDate
            };
        }

        public string Title { get; set; }
        public string Description { get; set; }
        public List<string> Subject { get; set; } = new();
        public List<string> Creator { get; set; } = new();
        public string Rights { get; set; }
        public DateTime? CreateDate { get; set; }
        public DateTime? ModifyDate { get; set; }
    }

    /// <summary>
    /// 文件系统数据
    /// </summary>
    public class FileSystemData
    {
        public Dictionary<string, object> ToDictionary()
        {
            return new Dictionary<string, object>
            {
                ["FileName"] = FileName,
                ["FileSize"] = FileSize,
                ["CreationTime"] = CreationTime,
                ["LastWriteTime"] = LastWriteTime,
                ["LastAccessTime"] = LastAccessTime,
                ["Attributes"] = Attributes,
                ["Extension"] = Extension,
                ["ContentType"] = ContentType
            };
        }

        public string FileName { get; set; }
        public long FileSize { get; set; }
        public DateTime CreationTime { get; set; }
        public DateTime LastWriteTime { get; set; }
        public DateTime LastAccessTime { get; set; }
        public string Attributes { get; set; }
        public string Extension { get; set; }
        public string ContentType { get; set; }
    }

    /// <summary>
    /// 图像技术数据
    /// </summary>
    public class ImageTechnicalData
    {
        public Dictionary<string, object> ToDictionary()
        {
            return new Dictionary<string, object>
            {
                ["Width"] = Width,
                ["Height"] = Height,
                ["ColorDepth"] = ColorDepth,
                ["ColorSpace"] = ColorSpace,
                ["Compression"] = Compression,
                ["Format"] = Format,
                ["PixelsPerInchX"] = PixelsPerInchX,
                ["PixelsPerInchY"] = PixelsPerInchY,
                ["HasAlpha"] = HasAlpha
            };
        }

        public int Width { get; set; }
        public int Height { get; set; }
        public int ColorDepth { get; set; }
        public string ColorSpace { get; set; }
        public string Compression { get; set; }
        public string Format { get; set; }
        public int PixelsPerInchX { get; set; }
        public int PixelsPerInchY { get; set; }
        public bool HasAlpha { get; set; }
    }

    /// <summary>
    /// 地理数据
    /// </summary>
    public class GeographicData
    {
        public Dictionary<string, object> ToDictionary()
        {
            return new Dictionary<string, object>
            {
                ["Latitude"] = Latitude,
                ["Longitude"] = Longitude,
                ["Altitude"] = Altitude,
                ["Country"] = Country,
                ["City"] = City,
                ["Address"] = Address,
                ["PlaceName"] = PlaceName,
                ["GPSAccuracy"] = GPSAccuracy
            };
        }

        public double? Latitude { get; set; }
        public double? Longitude { get; set; }
        public double? Altitude { get; set; }
        public string Country { get; set; }
        public string City { get; set; }
        public string Address { get; set; }
        public string PlaceName { get; set; }
        public double? GPSAccuracy { get; set; }
    }

    /// <summary>
    /// 相机数据
    /// </summary>
    public class CameraData
    {
        public Dictionary<string, object> ToDictionary()
        {
            return new Dictionary<string, object>
            {
                ["Make"] = Make,
                ["Model"] = Model,
                ["SerialNumber"] = SerialNumber,
                ["LensModel"] = LensModel,
                ["LensSerialNumber"] = LensSerialNumber,
                ["Firmware"] = Firmware
            };
        }

        public string Make { get; set; }
        public string Model { get; set; }
        public string SerialNumber { get; set; }
        public string LensModel { get; set; }
        public string LensSerialNumber { get; set; }
        public string Firmware { get; set; }
    }

    /// <summary>
    /// 软件数据
    /// </summary>
    public class SoftwareData
    {
        public Dictionary<string, object> ToDictionary()
        {
            return new Dictionary<string, object>
            {
                ["Software"] = Software,
                ["Version"] = Version,
                ["CreatorTool"] = CreatorTool,
                ["History"] = History,
                ["ModifyingSoftware"] = ModifyingSoftware
            };
        }

        public string Software { get; set; }
        public string Version { get; set; }
        public string CreatorTool { get; set; }
        public List<string> History { get; set; } = new();
        public string ModifyingSoftware { get; set; }
    }

    /// <summary>
    /// 版权数据
    /// </summary>
    public class CopyrightData
    {
        public Dictionary<string, object> ToDictionary()
        {
            return new Dictionary<string, object>
            {
                ["Copyright"] = Copyright,
                ["CopyrightNotice"] = CopyrightNotice,
                ["UsageTerms"] = UsageTerms,
                ["WebStatement"] = WebStatement,
                ["Rights"] = Rights
            };
        }

        public string Copyright { get; set; }
        public string CopyrightNotice { get; set; }
        public string UsageTerms { get; set; }
        public string WebStatement { get; set; }
        public string Rights { get; set; }
    }

    /// <summary>
    /// 视频数据
    /// </summary>
    public class VideoData
    {
        public Dictionary<string, object> ToDictionary()
        {
            return new Dictionary<string, object>
            {
                ["Duration"] = Duration,
                ["FrameRate"] = FrameRate,
                ["Bitrate"] = Bitrate,
                ["Codec"] = Codec,
                ["AudioCodec"] = AudioCodec,
                ["VideoCodec"] = VideoCodec,
                ["AudioChannels"] = AudioChannels,
                ["AudioSampleRate"] = AudioSampleRate
            };
        }

        public TimeSpan Duration { get; set; }
        public double FrameRate { get; set; }
        public long Bitrate { get; set; }
        public string Codec { get; set; }
        public string AudioCodec { get; set; }
        public string VideoCodec { get; set; }
        public int AudioChannels { get; set; }
        public int AudioSampleRate { get; set; }
    }

    /// <summary>
    /// 音频数据
    /// </summary>
    public class AudioData
    {
        public Dictionary<string, object> ToDictionary()
        {
            return new Dictionary<string, object>
            {
                ["Duration"] = Duration,
                ["Bitrate"] = Bitrate,
                ["SampleRate"] = SampleRate,
                ["Channels"] = Channels,
                ["Codec"] = Codec,
                ["Artist"] = Artist,
                ["Title"] = Title,
                ["Album"] = Album,
                ["Year"] = Year
            };
        }

        public TimeSpan Duration { get; set; }
        public long Bitrate { get; set; }
        public int SampleRate { get; set; }
        public int Channels { get; set; }
        public string Codec { get; set; }
        public string Artist { get; set; }
        public string Title { get; set; }
        public string Album { get; set; }
        public int Year { get; set; }
    }

    /// <summary>
    /// 数据完整性结果
    /// </summary>
    public class DataIntegrityResult
    {
        /// <summary>
        /// 是否完整
        /// </summary>
        public bool IsComplete { get; set; }

        /// <summary>
        /// 完整性评分（0-1）
        /// </summary>
        public double IntegrityScore { get; set; }

        /// <summary>
        /// 损坏的字段
        /// </summary>
        public List<string> CorruptedFields { get; set; } = new();

        /// <summary>
        /// 缺失的字段
        /// </summary>
        public List<string> MissingFields { get; set; } = new();

        /// <summary>
        /// 不一致的字段
        /// </summary>
        public List<string> InconsistentFields { get; set; } = new();

        /// <summary>
        /// 验证结果
        /// </summary>
        public Dictionary<string, ValidationResult> ValidationResults { get; set; } = new();

        /// <summary>
        /// 修复建议
        /// </summary>
        public List<string> RepairSuggestions { get; set; } = new();
    }

    /// <summary>
    /// 验证结果
    /// </summary>
    public class ValidationResult
    {
        /// <summary>
        /// 是否有效
        /// </summary>
        public bool IsValid { get; set; }

        /// <summary>
        /// 验证类型
        /// </summary>
        public string ValidationType { get; set; }

        /// <summary>
        /// 验证消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 验证时间
        /// </summary>
        public DateTime ValidationTime { get; set; }
    }
}